/*****************************************************************************
*                                                                           *
* Amazon S3 Command Shell  (s3shell)                                        *
*                                                                           *
* Copyright (C) 2008 Sumit Birla <http://sumitbirla.com>                    *
*                                                                           *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License for more details.                              *
*                                                                           *
* You should have received a copy of the GNU General Public License         *
* along with this program; if not, write to the Free Software               *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA  *
*                                                                           *
*****************************************************************************/

package com.sumitbirla.s3shell;

import java.io.*;
import java.util.*;
import java.util.prefs.*;
import java.text.SimpleDateFormat;
import com.amazon.s3.AWSAuthConnection;


public class Main 
{    
    public static void main(String[] args) {
        
        System.out.println("Welcome to the Java S3 Shell");
        System.out.println("----------------------------");
        
        // setup environment variables
        Environment env = new Environment();
        env.prefs = Preferences.userRoot().node("/com/sumitbirla/s3shell");
        env.account = env.prefs.get("last_account", "");
        env.bucket = env.prefs.get("last_bucket", "");
        env.dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm z");
        
        // load last used accountlsbucket
        
        if (!env.account.equals("")) {
            Acct account = new Acct(env);
            String params[] = { "acct", "use", env.account };
            account.execute(params, System.in, System.out);
        }
        
        // open up standard input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        // command factory to retrieve an instance of an appropriate handler
        CommandFactory factory = new CommandFactory();
        
        // main loop to read in commands and execute them
        while (true) {
            try {
                System.out.print("s3shell> ");
                String line = br.readLine();
                String[] tokens = line.split("\\s");
                if (tokens.length < 1)
                    continue;
                
                if (tokens[0].equals("exit"))
                    break;
                
                Command cmd = factory.getCommand(tokens, env);
                cmd.execute(tokens, System.in, System.out);
                
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        
        System.out.println("Goodbye.\n\n");
    }
}
